<template>
  <a-modal
    title="奖惩信息"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    okText="确认"
    cancelText="取消"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form
        :form="form"
        v-bind="formLayout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="员工">
              <a-select
                placeholder="请选择员工"
                v-decorator="['staffId',{ rules: [{ required: true, message: '请选择员工' }] }]">
                <a-select-option
                  v-for="row in allUsers"
                  :key="row.userId"
                  :value="row.userId">{{ row.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="兑现日期">
              <a-date-picker
                v-decorator="['execDate',{ rules: [{ required: true, message: '请选择执行日期' }] }]"
                valueFormat="YYYY-MM-DD hh:mm:ss"
                placeholder="选择日期" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="事迹">
              <a-input placeholder="请输入事迹" v-decorator="['deed',{ rules: [{ required: true, message: '请输入事迹' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="类型">
              <a-select
                placeholder="请选择类型"
                v-decorator="['type',{ rules: [{ required: true, message: '请选择类型' }] }]">
                <a-select-option :value="1">奖励
                </a-select-option>
                <a-select-option :value="2">惩罚
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="状态">
              <a-select
                placeholder="请选择状态"
                v-decorator="['status',{ rules: [{ required: true, message: '请选择状态' }] }]">
                <a-select-option :value="0">不公开
                </a-select-option>
                <a-select-option :value="1">公开
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="处理结果">
              <a-input placeholder="请输入处理结果" v-decorator="['execResult',{ rules: [{ required: true, message: '请输入处理结果' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="信息有效期">
              <a-range-picker
                :placeholder="['开始日期','结束日期']"
                v-decorator="['validDate',{ rules: [{ required: true, message: '请选择信息展示有效期' }] }]"
                valueFormat="YYYY-MM-DD hh:mm:ss"
                style="width: 300px" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注">
              <a-input v-decorator="['note']" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['id', 'staffName', 'createDate', 'deed', 'type', 'execResult', 'execDate', 'validTime', 'status', 'note']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    allUsers: {
      type: Array,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    console.log('')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
